var loginWindow = new Ext.Window({
    width: 250,
    height: "auto",
    title: "Autentificare",
    modal: true,
    closable: true,
	margins:'5 5 5 5'
})

var loginForm = new Ext.FormPanel({
    id: 'loginForm',
    standardSubmit: false,
    width: 'auto',
    autoScroll: true,
    labelWidth: 100,
    defaultType: "textfield",
    items: [{
        fieldLabel: 'Email',
        name: 'email',
		vtype: 'email'
    }, {
        fieldLabel: 'Parola',
        name: 'password',
		inputType: 'password'
    }],
    buttons: [new Ext.Button({
        text: 'Salveaza',
        handler: function(){
            loginForm.getForm().doAction('submit', {
                url: 'server/UserLogin.php',
                //success: validateLogin,
				success: OpenApplication,
				failure: function(){
					Ext.MessageBox.alert('Error', 'Invalid authentication data.')
				}
            })
        }
    })]
});

var proxy = new Ext.data.ScriptTagProxy({
    url: 'server/UserDomains.php'
});

function validateLogin(sender, response){
    if (response.result.success == true) {
        loginWindow.remove(loginForm);
        userID = response.result.userID;
        var store = new Ext.data.Store({
            proxy: proxy,
            reader: new Ext.data.JsonReader({
                id: 'ID',
                totalProperty: 'totalCount',
                root: 'Names'
            }, [{
                name: 'ID'
            }, {
                name: 'name'
            }])
        });
        var domainForm = new Ext.form.FormPanel({
            id: 'domainForm',
            standardSubmit: false,
            width: 'auto',
            autoScroll: true,
            labelWidth: 100,
            defaultType: "textfield",
            items: [{
                store: store,
                fieldLabel: 'ComboBox',
                displayField: 'name',
                valueField: 'name',
                typeAhead: true,
                forceSelection: true,
                mode: 'remote',
                triggerAction: 'all',
                selectOnFocus: true,
                editable: true,
                xtype: 'combo',
                width: 130,
                emptyText: 'Select a domain...',
                listeners: {
                
                    // 'change' will be fired when the value has changed and the user exits the ComboBox via tab, click, etc.
                    // The 'newValue' and 'oldValue' params will be from the field specified in the 'valueField' config above.
                    change: function(combo, newValue, oldValue){
                        //alert("change");
                        //console.log("Old Value: " + oldValue);
                        //console.log("New Value: " + newValue);
                    },
                    
                    // 'select' will be fired as soon as an item in the ComboBox is selected with mouse, keyboard.
                    select: function(combo, record, index){
                        //alert(domainForm.getComponent("selection"));
                        domainForm.getComponent("selectedDomain").setRawValue(record.data.ID);
						//alert(domainForm.getComponent("selection").value);
                        //console.log(record.data.name);
                        //console.log(index);
                        //console.log(record.data.ID);
                    }
                }
            }, {
                id: 'selectedDomain',
                name: 'selectedDomain',
                xtype: 'hidden',
				value:"notYetSet"
            }, {
                id: 'userID',
                name: 'userID',
                xtype: 'hidden',
				value:userID
            }],
            buttons: [new Ext.Button({
                text: 'Salveaza',
                handler: function(){
                    domainForm.getForm().doAction('submit', {
                        url: 'server/SetWorkingDomain.php',
                        success: OpenApplication
                    })
                }
            })]
        });
        loginWindow.add(domainForm);
        loginWindow.render();
        //loginWindow.add(combo);
        //alert(domains);
    }
}

function OpenApplication()
{
	window.location.href = "index.php";
}

Ext.onReady(function(){
    loginWindow.add(loginForm)
    loginWindow.show();
});
